package com.xiaoying.chuguoliuxue;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.ColorInt;
import android.support.annotation.DrawableRes;
import android.support.annotation.LayoutRes;
import android.support.annotation.StringRes;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.xiaoying.chuguoliuxue.widget.LoadingDialog;

import butterknife.ButterKnife;


/**
 * 类名：MVPBaseActivity <br/>
 * 描述：MVP结构的Activity
 * 创建时间：2016/03/17 18:45
 *
 * @author hanter
 * @version 1.0
 */
public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener {

    // 标题栏视图
    protected ViewGroup mTitleBar;
    protected View mTitleBarLine;
    protected ViewGroup mLeftBar;
    protected ViewGroup mRightBar;
    protected TextView mTitle;
    protected TextView mLeftBarButton;
    protected TextView mRightBarButton;
    // 内容视图
    protected FrameLayout mContentLayout;

    protected Activity mContext;
    protected View mContentView;
    private LoadingDialog mLoadingDialog;
    public static final String TAG_LOADING_DIALOG = BaseActivity.class.getName() + ".loadingdialog";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base_status_bar);
        // 初始化
        initBaseContentView();
        ButterKnife.bind(this);
        mContext = this;
        mLeftBar.setOnClickListener(this);
        mRightBar.setOnClickListener(this);
    }

    /**
     * 初始化视图
     */
    private void initBaseContentView() {
        // 标题栏视图
        mTitleBar = (ViewGroup) findViewById(R.id.rl_title_bar);
        mTitleBarLine = findViewById(R.id.tv_title_bar_line);
        mLeftBar = (ViewGroup) findViewById(R.id.ll_base_left_titlebar_container);
        mRightBar = (ViewGroup) findViewById(R.id.ll_base_right_titlebar_container);
        mTitle = (TextView) findViewById(R.id.tv_base_center_titlebar);
        mLeftBarButton = (TextView) findViewById(R.id.tv_base_left_titlebar);
        mRightBarButton = (TextView) findViewById(R.id.tv_base_right_titlebar);
        mContentLayout = (FrameLayout) findViewById(R.id.base_content_container);
        onAddContent();
    }

    protected void onAddContent() {
        mContentView = LayoutInflater.from(this).inflate(getLayout(), mContentLayout, false);
        if (mContentView != null) {
            mContentLayout.addView(mContentView);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    protected abstract
    @LayoutRes
    int getLayout();

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_base_left_titlebar_container:
                onBackPressed();
                break;
        }
    }

    public void setTitle(String title) {
        mTitle.setText(title);
    }

    public void setTitle(@StringRes int resId) {
        mTitle.setText(resId);
    }

    public void setTitleBackground(int resId) {
        mTitleBar.setBackgroundResource(resId);
    }

    public void setColorPrimary(@ColorInt int color) {
        mTitleBar.setBackgroundColor(color);
    }

    /**
     * 设置标题栏可见
     *
     * @param visibility visibility
     */
    public void setTitleBarVisibility(int visibility) {
        mTitleBar.setVisibility(visibility);
        mTitleBarLine.setVisibility(visibility);
    }

    public void setLeftTitleText(String title) {
        mLeftBarButton.setText(title);
    }

    public void setLeftTitleText(@StringRes int resId) {
        mLeftBarButton.setText(resId);
    }

    public void setRightTitleText(String title) {
        mRightBarButton.setText(title);
    }

    public void setRightTitleTextColor(int color) {
        mRightBarButton.setTextColor(color);
    }

    public void setRightTitleText(@StringRes int resId) {
        mRightBarButton.setText(resId);
    }

    public void setLeftTitleImage(@DrawableRes int resId) {
        mLeftBarButton.setCompoundDrawablesWithIntrinsicBounds(resId, 0, 0, 0);
    }

    public void setRightTitleImage(@DrawableRes int resId) {
        mRightBarButton.setCompoundDrawablesWithIntrinsicBounds(0, 0, resId, 0);
    }

    public void addLeftButton(String text) {
        Button button = new Button(this);
        button.setText(text);
        mLeftBar.addView(button);
    }

    public Button addRightButton() {
        Button button = new Button(this);
        button.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.ic_sousuo, 0);
        button.setMaxWidth(60);
        button.setMinWidth(0);
        button.setBackgroundColor(0x00000000);
        mRightBar.addView(button);
        ViewGroup.LayoutParams paras = button.getLayoutParams();
        paras.width = 120;
        button.setLayoutParams(paras);
        return button;
    }

    private void showProgressDialog() {
        if (mLoadingDialog == null)
            mLoadingDialog = LoadingDialog.newInstance();
        mLoadingDialog.show(getSupportFragmentManager(), TAG_LOADING_DIALOG);
    }

    private void dismissLoadingDialog() {
        if (mLoadingDialog != null) {
            mLoadingDialog.dismiss();
            mLoadingDialog = null;
        }
    }

    public void time2000() {
        showProgressDialog();
        new Handler().postDelayed(new Runnable() {
            public void run() {
                dismissLoadingDialog();
            }
        }, 1500);
    }

}
